package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class ListItem extends Sprite implements IChangeSize
	{
		private var _sizeChangedEventObject:EventObject; //Event
		private var _current:DisplayObject;
		private var _unselected:DisplayObject;
		private var _selected:DisplayObject;
		private var _over:DisplayObject;
		private var _tag:Object;
		private var _isSelected:Boolean;
		private var _isOver:Boolean;
		
		private var _desiredWidth:Number;
		private var _desiredHeight:Number;
		
		public function ListItem(unselected:DisplayObject, selected:DisplayObject, over:DisplayObject = null)
		{
			this._sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");
			this._unselected = unselected;
			this._selected = selected;
			if (over == null)
			{
				this._over = this._unselected;
			}
			else
			{
				this._over = over;
			}
			//Assume not selected first.
			this._isSelected = false;
			this._isOver = false;
			synchronizeGraphics();
		}
		
		//IChangeSize -->
		public function get sizeChangedEventObject():EventObject
		{
			return this._sizeChangedEventObject;			
		}
		//<-- IChangeSize
		
		public function set tag(value:Object):void
		{
			this._tag = value;
		}
		
		public function get tag():Object
		{
			return this._tag;
		}
		
		public override function set height(value:Number):void
		{	
			if (value != this._desiredHeight)
			{
				this._desiredHeight = value;
				this._unselected.height = this._desiredHeight;
				this._selected.height = this._desiredHeight;
				this._over.height = this._desiredHeight;
			}
		}
		
		public override function set width(value:Number):void
		{
			if (value != this._desiredWidth)
			{
				this._desiredWidth = value;
				this._unselected.width = this._desiredWidth;
				this._selected.width = this._desiredWidth;	
				this._over.width = this._desiredWidth;
			}
		}
		
		public function get isSelected():Boolean
		{
			return this._isSelected;	
		}
		
		public function set isSelected(value:Boolean):void
		{
			if (this._isSelected != value)
			{	
				this._isSelected = value;
				synchronizeGraphics();
			}
		}
		
		public function get isOver():Boolean
		{
			return this._isOver;
		}
		
		public function set isOver(value:Boolean):void
		{
			if (this._isOver != value)
			{
				this._isOver = value;
				synchronizeGraphics();
			}
		}
		
		private function synchronizeGraphics():void
		{
			var current:DisplayObject;
			if (this._isSelected)
			{
				current = this._selected;	
			}
			else if (this._isOver)
			{
				current = this._over;
			}
			else
			{
				current = this._unselected;
			}
			
			if (this._current != current)
			{
				//Remove the old one
				var previous:DisplayObject = this._current;
				if (previous != null)
				{
					this.removeChild(previous);
				}
				//Set the new one
				this._current = current;
				if (this._current != null)
				{
					this.addChild(this._current);
				}
				//If height changed 
				if ((this._current == null && previous != null) || (this._current != null && previous == null) || (this._current.height != previous.height))
				{
					this._sizeChangedEventObject.dispatchEvent();
				}
			}
		}
	}
}